<?php

namespace App\Http\Controllers\Admin\Article;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CategoryController extends Controller
{
    //
    public function gets(){
        $ret=DB::table('category')->where('status','=',1)->get();
        return  $this->ajax(true,$ret);
    }
    public function add(Request $request){
        $data=$request->input();
        if(isset($data['val'])&&!empty($data['val'])){
            $isOn=DB::table('category')->where('name','=',trim($data['val']))->count();
             if($isOn!==0){
                 return  $this->ajax(false,'添加失败,分类已存在');
             }
             $id=DB::table('category')->insertGetId(['name'=>trim($data['val'])]);
             if($id){
                 return  $this->ajax(true,'添加成功');
             }
        }
        return  $this->ajax(false,'添加失败');
    }
    public function del(Request $request){
        $data=$request->input();
        if(isset($data['id'])&&!empty($data['id'])) {
            $id=DB::table('category')->where(['id'=>intval($data['id'])])->delete();
            if($id){
                return  $this->ajax(true,'删除成功');
            }
        }
        return  $this->ajax(false,'删除失败');
    }
}
